package com.youku.plugins.commonui.view
{
   import flash.display.MovieClip;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.PlayerConfig;
   import com.youku.events.CoreEvent;
   import com.youku.events.PlayerControlEvent;
   import com.youku.plugins.commonui.CommonUIEvent;
   import com.youku.events.PluginEvent;
   import com.youku.utils.OpenWindow;
   import flash.net.URLRequest;
   import com.youku.plugins.commonui.CommonUIConstant;
   import com.youku.data.PlayerConstant;
   
   public class AlertBoxMediator extends Object
   {
      
      public function AlertBoxMediator(param1:MovieClip, param2:IPlayerProxy) {
         super();
         this._mainMc = param1;
         this._playerProxy = param2;
         this._alertBox = new AlertBoxV4();
         this._isShowing = false;
         this._shutData = false;
         this.Init();
      }
      
      private var _mainMc:MovieClip;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _alertBox:AlertBoxV4;
      
      private var _from:String;
      
      private var _type:String;
      
      private var _parentWidth:Number;
      
      private var _parentHeight:Number;
      
      private var _1080PInfo:String = "尊敬的用户:\n\n1080P画质仅<a href=\"event:refresh\"><font color=\"#22B8FE\"><u>影视会员专享</u></font></a>!\n\n开通影视会员即可免费观看。";
      
      private var _1080PLink:String = "http://cps.youku.com/redirect.html?id=00000321";
      
      private var _accInfo:String = "\n\n亲：会员尊享带宽加速特权已上线，VIP会员可免费享用！";
      
      private var _accLink:String = "http://cps.youku.com/redirect.html?id=000053c9";
      
      private var _isShowing:Boolean;
      
      private var _shutData:Boolean;
      
      private function Init() : void {
         this._parentWidth = PlayerConfig.normalWidth;
         this._parentHeight = PlayerConfig.normalHeight;
         this._playerProxy.addEventListener(CoreEvent.PAUSE,this.onPlayerPause);
         this._playerProxy.addEventListener(CoreEvent.RESUME,this.onPlayerResume);
         this._playerProxy.addEventListener(PlayerControlEvent.BEFORE_SHOW_PANELS,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.SHOW_ALERT_BOX,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.HIDE_ALERT_BOX,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.SHOW_END_CARD,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.PLAYER_SIZE_CHANGED,this.onPlayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.FULLSCREEN_CHANGED,this.onPlayerEvent);
         this._alertBox.addEventListener(CommonUIEvent.ALERT_CANCEL_CLICK,this.onCommonUIEvent);
         this._alertBox.addEventListener(CommonUIEvent.ALERT_CONFIRM_CLICK,this.onCommonUIEvent);
         this._alertBox.addEventListener(CommonUIEvent.ALERT_LINK_CLICK,this.onCommonUIEvent);
      }
      
      private function onCommonUIEvent(param1:CommonUIEvent) : void {
         switch(param1.type)
         {
            case CommonUIEvent.ALERT_CANCEL_CLICK:
            case CommonUIEvent.QRCODE_LOAD_ERROR:
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.ALERT_BOX_HIDE,
                  {
                     "type":this._type,
                     "from":this._from
                  }));
               break;
            case CommonUIEvent.ALERT_CONFIRM_CLICK:
            case CommonUIEvent.ALERT_LINK_CLICK:
               this._playerProxy.dispatchEvent(new PlayerControlEvent(PlayerControlEvent.CORE_PAUSE,{"useraction":true}));
               if(this._type == "1080P")
               {
                  OpenWindow.open(new URLRequest(this._1080PLink));
               }
               else if(this._type == "acc")
               {
                  OpenWindow.open(new URLRequest(this._accLink));
               }
               
               break;
            case CommonUIEvent.QRCODE_LOADED:
               this.show(this._type);
               break;
         }
      }
      
      private function onPlayerPause(param1:CoreEvent) : void {
         if(param1.data.useraction)
         {
            this._shutData = false;
         }
      }
      
      private function onPlayerResume(param1:CoreEvent) : void {
         this._shutData = true;
      }
      
      private function onPlayerEvent(param1:PlayerControlEvent) : void {
         var _loc2_:* = false;
         var _loc3_:* = NaN;
         var _loc4_:* = NaN;
         var _loc5_:* = NaN;
         var _loc6_:String = null;
         switch(param1.type)
         {
            case PlayerControlEvent.SHOW_ALERT_BOX:
               if((this._isShowing) && this._type == param1.data.type && this._from == param1.data.from)
               {
                  this.hide();
               }
               else
               {
                  this._from = param1.data.from;
                  this._type = param1.data.type;
                  this.reset(this._type);
               }
               break;
            case PlayerControlEvent.HIDE_ALERT_BOX:
               if(this._type == param1.data.type && this._from == param1.data.from)
               {
                  this.hide();
               }
               break;
            case PlayerControlEvent.SHOW_END_CARD:
               this.hide();
               break;
            case PlayerControlEvent.PLAYER_SIZE_CHANGED:
            case PlayerControlEvent.FULLSCREEN_CHANGED:
               _loc2_ = param1.data.fullscreen;
               _loc3_ = param1.data.width;
               _loc4_ = param1.data.height;
               _loc5_ = param1.data.cheight;
               this._parentWidth = _loc3_;
               this._parentHeight = _loc2_?_loc4_:_loc4_ - _loc5_;
               this._alertBox.setSize(CommonUIConstant.ALERT_BOX_WIDTH,CommonUIConstant.ALERT_BOX_HEIGHT);
               this.updateSize();
               break;
            case PlayerControlEvent.COMMON_RESET:
               this.hide();
               break;
            case PlayerControlEvent.BEFORE_SHOW_PANELS:
               _loc6_ = param1.data.panel;
               if(_loc6_ != PlayerConstant.PANEL_ALERT)
               {
                  this.hide();
               }
               break;
         }
      }
      
      private function reset(param1:String) : void {
         switch(param1)
         {
            case "1080P":
               this._alertBox.reset(this._1080PInfo,"立即开通","继续播放");
               this.show(param1);
               break;
            case "QRCode":
               break;
            case "acc":
               this._alertBox.reset(this._accInfo,"立即享用","关闭");
               this.show(param1);
               break;
         }
      }
      
      private function updateSize() : void {
         var _loc1_:Number = 1;
         var _loc2_:Number = 1;
         if(this._parentWidth < CommonUIConstant.ALERT_BOX_WIDTH || this._parentHeight < CommonUIConstant.ALERT_BOX_HEIGHT)
         {
            if(this._parentWidth / this._parentHeight < CommonUIConstant.ALERT_BOX_WIDTH / CommonUIConstant.ALERT_BOX_HEIGHT)
            {
               _loc1_ = _loc2_ = this._parentWidth / CommonUIConstant.ALERT_BOX_WIDTH;
            }
            else
            {
               _loc1_ = _loc2_ = this._parentHeight / CommonUIConstant.ALERT_BOX_HEIGHT;
            }
         }
         this._alertBox.scaleX = _loc1_;
         this._alertBox.scaleY = _loc2_;
         this.updateLayout();
      }
      
      private function updateLayout() : void {
         this._alertBox.x = Math.floor((this._parentWidth - this._alertBox.width) / 2);
         this._alertBox.y = Math.floor((this._parentHeight - this._alertBox.height) / 2);
      }
      
      private function show(param1:String) : void {
         this._alertBox.visible = false;
         switch(param1)
         {
            case "1080P":
               if(!this._mainMc.contains(this._alertBox))
               {
                  this._mainMc.addChild(this._alertBox);
               }
               this._alertBox.visible = true;
               this.updateSize();
               this._isShowing = true;
               break;
            case "QRCode":
               if(this._from == "pauseAD" && (this._shutData))
               {
                  return;
               }
               this.updateSize();
               this._isShowing = true;
               break;
            case "acc":
               if(!this._mainMc.contains(this._alertBox))
               {
                  this._mainMc.addChild(this._alertBox);
               }
               this._alertBox.visible = true;
               this.updateSize();
               this._isShowing = true;
               break;
         }
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.PANEL_STATE_CHANGE,
            {
               "type":this._type,
               "state":this._isShowing,
               "from":this._from
            }));
      }
      
      private function hide() : void {
         if(this._isShowing)
         {
            if(this._mainMc.contains(this._alertBox))
            {
               this._mainMc.removeChild(this._alertBox);
            }
            this._alertBox.visible = false;
            this._isShowing = false;
            this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.PANEL_STATE_CHANGE,
               {
                  "type":this._type,
                  "state":this._isShowing,
                  "from":this._from
               }));
         }
      }
   }
}
